
$(document).ready(function() {
	//获取数据
	var postData = {}; 

    //发送报文
    $.axse(
        "getSearchList",
        postData,
        function(d){
        	if(d.code == 'J000000'){
    			var searchModel = d.resultMap.searchListModel;
    			initSearchDate(searchModel);
    		}else {
    			alert("内部错误，加载搜索数据失败");
    		} 
        }, 
        function(e){
        	 alert('网络繁忙，加载搜索数据失败\n\n'+ e);
        }
    );


 
});


function initSearchDate(data){
	var jobNames= new Array();
	var scheduleNames= new Array();
	var jobNameSet = data.jobNames;
	var scheduleSet = data.scheduleNames;
	jobNameSet.forEach(function (item) {	
		var job = new Object;
		job.label = item;

		jobNames.push(job);
	});
	
	$('#nope').autocompleter({
	    // marker for autocomplete matches
	    highlightMatches: true,
	
	    // object to local or url to remote search
	    source: jobNames,
	
	    // custom template
	    template: '{{ label }}',
	
	    // show hint
	    hint: true,
	
	    // abort source if empty field
	    empty: false,
	
	    // max results
	    limit: 5,
	
	    callback: function (value, index, selected) {
	        if (selected) {
	            $('.icon').css('background-color', selected.hex);
	        }
	    }
	});
	
	
	
	scheduleSet.forEach(function (item) {	
		var schedule = new Object;
		schedule.label = item;

		scheduleNames.push(schedule);
	});
	
	$('#nope1').autocompleter({
	    // marker for autocomplete matches
	    highlightMatches: true,
	
	    // object to local or url to remote search
	    source: scheduleNames,
	
	    // custom template
	    template: '{{ label }}',
	
	    // show hint
	    hint: true,
	
	    // abort source if empty field
	    empty: false,
	
	    // max results
	    limit: 5,
	
	    callback: function (value, index, selected) {
	        if (selected) {
	            $('.icon').css('background-color', selected.hex);
	        }
	    }
	});
}


function DeleteJob(schedName, triggerName, triggerGroup, jobName, jobGroup){
	
	function  sendDeletePacket(){
    	var postData = {
    			"schedName":schedName,
				"triggerName":triggerName,
				"triggerGroup":triggerGroup,
				"jobName":jobName,
				"jobGroup":jobGroup}; 
	
	    //发送报文
	    $.axse(
	        "deleteJob",
	        postData,
	        function(d){
	             manageSuc(d, "删除");
	        }, 
	        function(e){
	        	Dialog.alert('网络繁忙\n\n'+ e);
	        }
	    );

	}
	
	confirmJDialog("确定要删除该任务吗?", sendDeletePacket, null);
}

function  showJobDetail(schedName, triggerName, triggerGroup,jobName, jobGroup){
	
	var postData  = {
				"schedName":schedName,
				"triggerName":triggerName,
				"triggerGroup":triggerGroup,
				"jobName":jobName,
				"jobGroup":jobGroup}; 

	$.axse(
		"getJobDetail",
		postData,
		function(d){
			if(d.code == 'J000000'){
				var jobDetail = d.resultMap.jobDetail;
				
				setJobDetail(jobDetail);
				var option={
						height:'400px',
						animationspeed:100
					};
				
				openReveal(true,$('#showOneModal'),option);
				
				

			}else {
				var des = data.resultMap.errDes;
				alert("获取详情失败，失败原因：" + des);
			} 	
		}, 
		function(e){
			alert('网络繁忙\n\n'+ e);
		}
	);
		
}

function setJobDetail(job){
	
	$("#detail_scheduleName").val(job.schedName);
	$("#detail_className").val(job.jobClassName);
	$("#detail_taskName").val(job.jobName);
	$("#detail_taskGrpName").val(job.jobGroup);
	$("#detail_tragerName").val(job.triggerName);
	$("#detail_tragerGrpName").val(job.triggerGroup);
	$("#detail_prevFireTime").val(job.prevFireTime);
	$("#detail_nextFireTime").val(job.nextFireTime);
	$("#detail_startAt").val(job.startAt);
	$("#detail_endAt").val(job.endAt);
	if(job.isRecovery=="false"){
		$("#detail_isRecovery").val('否');
	}else{
		$("#detail_isRecovery").val('是');
	}
	$("#detail_status").val(job.status);
	$("#detail_expression").val(job.expression);
	$("#detail_cronDescription").html(job.cronDescription);

	if(job.fullJobClassName== undefined ||job.fullJobClassName==""){
		$("#runinfodiv1").hide();
		$("#runinfodiv2").show();
	}else{
		$("#runinfodiv2").hide();
		$("#runinfodiv1").show();
		
		$("#runing_id").html(job.id);
		$("#runing_instanceName").html(job.instanceName);
		$("#runing_ip").html(job.ip);
		$("#runing_hostName").html(job.hostName);
		$("#runing_startTime").html(job.startTime);
		$("#runing_endTime").html(job.endTime);
		$("#runing_cost").html(job.cost);
		$("#runing_fullJobClassName").html(job.fullJobClassName);
		$("#runing_result").html(job.result);	
	}

}
function PauseJob(schedName, triggerName, triggerGroup,jobName, jobGroup){
	var postData  = {
			"schedName":schedName,
		   	"triggerName":triggerName,
		   	"triggerGroup":triggerGroup,
		   	"jobName":jobName,
		   	"jobGroup":jobGroup}; 
	
    $.axse(
        "pauseJob",
        postData,
        function(d){
               manageSuc(d, "暂停");
        }, 
        function(e){
        	alert('网络繁忙\n\n'+ e);
        }
    );
       
	
	
	}
function RestartJob(schedName, triggerName, triggerGroup,jobName, jobGroup){
		
	var postData = {
					"schedName":schedName,
					"triggerName":triggerName,
					"triggerGroup":triggerGroup,
					"jobName":jobName,
					"jobGroup":jobGroup}; 
	 $(function(){
            $.axse(
                "restartjob",
                postData,
                function(d){
                    manageSuc(d, "重启");
                }, 
                function(e){
                	alert('网络繁忙\n\n'+ e);
                }
            );
        });
	}

function manageSuc(data, doType){
	if(data.code == 'J000000'){
			alertJDialog(doType+"成功", reload);	

		}else {
			
			var des = data.resultMap.errDes;
			alert("信息修改失败" + des);
		} 
	}
	
function searchJob(){
  	var key_taskname =document.getElementById("nope").value;
  	var key_schedname =document.getElementById("nope1").value;
  	window.location.href='./index?key_taskname='+key_taskname+'&key_schedname='+key_schedname;
}
	

function showExDetails(self){
		
	var desob = $(self).prev();
	var corn = desob.html();
	var postData = {"expression":corn}; 
	
	$.axse(
	    "showExDes",
	    postData,
	    function(d){
	    	if(d.code == 'J000000'){
	    		tipJDialog(d.resultMap.exdes, desob);
	    	//	showMessage("规则解析", d.resultMap.exdes);
	    	}
	    	else{
	    		tipJDialog("解析错误", desob);
	    	}
	    }, 
	    function(e){
	    	tipJDialog("网络错误", desob);
	    }
	);
			
				
}
function reload(){
	location.reload();
}